﻿<MyControl:SelectableItemControl x:Class="UmlControlLibrary.UmlDiagramControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:MyControl="clr-namespace:UmlControlLibrary"
    x:Name="Diagram"
    HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RenderOptions.CachingHint="Cache">

    <Control.Resources>              
        <ControlTemplate x:Key="ScrollViewerTemplate" TargetType="{x:Type ScrollViewer}">
            <Grid x:Name="Grid" Background="{TemplateBinding Background}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Rectangle x:Name="Corner" Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Grid.Column="1" Grid.Row="1"/>
                <ScrollContentPresenter Margin="{TemplateBinding Padding}" x:Name="PART_ScrollContentPresenter" Grid.Column="0" Grid.Row="0" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False"/>
                <ScrollBar Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Cursor="Arrow" x:Name="PART_VerticalScrollBar" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Value="{Binding Path=VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" Grid.Column="1" Grid.Row="0" ViewportSize="{TemplateBinding ViewportHeight}" AutomationProperties.AutomationId="VerticalScrollBar" ValueChanged="PART_VerticalScrollBar_ValueChanged" />
                <ScrollBar Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Cursor="Arrow" x:Name="PART_HorizontalScrollBar" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Value="{Binding Path=HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" Grid.Column="0" Grid.Row="1" Orientation="Horizontal" ViewportSize="{TemplateBinding ViewportWidth}" AutomationProperties.AutomationId="HorizontalScrollBar" ValueChanged="PART_VerticalScrollBar_ValueChanged"/>
            </Grid>
        </ControlTemplate>

        <!-- ResizeDecorator Template -->
        <ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="{x:Type Control}">
            <Grid>
                <Rectangle Stroke="Black" StrokeThickness="2" Margin="5 5 9 9" />
                <MyControl:ResizeThumb Width="7" Height="7" Cursor="SizeNS" Margin="0 1 0 0"
                       Position="Top"  VerticalAlignment="Top" HorizontalAlignment="Center"/>
                <MyControl:ResizeThumb Width="7" Height="7" Cursor="SizeWE" Margin="1 0 0 0"
                       Position="Left" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                <MyControl:ResizeThumb Width="7" Height="7" Cursor="SizeWE" Margin="0 0 5 0"
                       Position="Right" VerticalAlignment="Center" HorizontalAlignment="Right"/>
                <MyControl:ResizeThumb Width="7" Height="7" Cursor="SizeNS" Margin="0 0 0 5"
                       Position="Bottom" VerticalAlignment="Bottom" HorizontalAlignment="Center"/>
                <MyControl:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="1 1 0 0"
                       Position="TopLeft" VerticalAlignment="Top" HorizontalAlignment="Left"/>
                <MyControl:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="0 1 5 0"
                       Position="TopRight" VerticalAlignment="Top" HorizontalAlignment="Right"/>
                <MyControl:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="1 0 0 5"
                       Position="BottomLeft" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
                <MyControl:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="0 0 5 5"
                       Position="BottomRight" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
            </Grid>
        </ControlTemplate>

        <!-- Draggable Class Template -->
        <!--ControlTemplate x:Key="DragClassTemplate" TargetType="{x:Type Control}">
            <Grid>
                <MyControl:UmlDesignerElement Name="UmlClassView" DataContext="{Binding Path=DataContext}" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Margin="6 6 6 6" />
            </Grid>
        </ControlTemplate-->

        <!-- Draggable Interface Template -->
        <!--ControlTemplate x:Key="DragInterfaceTemplate" TargetType="{x:Type Control}">
            <Grid>
                <MyControl:UmlInterfaceControl Name="UmlInterfaceView" DataContext="{Binding Path=DataContext}" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Margin="6 6 6 6" />
            </Grid>
        </ControlTemplate-->

        <!-- class / interface / enum / ... Item -->
        <ControlTemplate x:Key="DraggableAndResizableDesignerItem" TargetType="MyControl:UmlControl">
            <Grid x:Name="UmlControlRootGrid" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"  >
                <MyControl:DragThumb Name="DraggableClassView"  />
                <!--Template="{StaticResource DragClassTemplate}"-->
                <Control Name="resizer" Template="{StaticResource ResizeDecoratorTemplate}" Visibility="Hidden" />
            </Grid>
        </ControlTemplate>

    </Control.Resources>
    <Canvas SizeChanged="Canvas_SizeChanged" Focusable="False" >
               
        <ScrollViewer Name="ScrollViewer1" Width="500" Height="500" CanContentScroll="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" SnapsToDevicePixels="True" Focusable="False" Drop="rootCanvas_Drop" AllowDrop="True" Template="{StaticResource ScrollViewerTemplate}"  MouseWheel="Canvas_MouseWheel">           
            <Canvas Name="rootCanvas" Width="{Binding ElementName=Diagram,Path=WorkspaceSize.X}" Height="{Binding ElementName=Diagram,Path=WorkspaceSize.Y}" MouseWheel="Canvas_MouseWheel" >
                <Canvas.Background>
                    <VisualBrush x:Name="rootbackground" TileMode="Tile" Viewport="0,0,40,40" ViewportUnits="Absolute" Viewbox="0,0,40,40"
ViewboxUnits="Absolute" RenderOptions.CachingHint="Cache">
                        <VisualBrush.Visual>
                            <Canvas>
                                <Rectangle Stroke="#E0E0E0" StrokeThickness="0.25" Width="40" Height="40" Fill="#F9F9F9"/>
                                <Line X1="0" Y1="20" X2="40" Y2="20" Stroke="#808080" StrokeThickness="0.25" />
                                <Line X1="20" Y1="0" X2="20" Y2="40" Stroke="#808080" StrokeThickness="0.25" />
                            </Canvas>
                        </VisualBrush.Visual>
                    </VisualBrush>
                </Canvas.Background>
                <Canvas.LayoutTransform>
                    <ScaleTransform x:Name="uiZoomTransform" />
                    <!--                        ScaleX="{Binding ElementName=uiZoomSlider, Path=Value}" 
                        ScaleY="{Binding ElementName=uiZoomSlider, Path=Value}" /-->
                </Canvas.LayoutTransform>
                <Rectangle Name="WorkSpaceLimit" Width="{Binding ElementName=Diagram,Path=WorkspaceSize.X}" Height="{Binding ElementName=Diagram,Path=WorkspaceSize.Y}" Stroke="Blue" StrokeThickness="2"/>
                <Rectangle Name="HilightLayer"
                           Canvas.Left="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Canvas}},Path=Left}" 
                           Canvas.Top="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Canvas}},Path=Top}" 
                           Width="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Canvas}},Path=ActualWidth}" 
                           Height="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Canvas}},Path=ActualHeight}" 
                           Fill="#20FFFF60" Stretch="Fill"
                           Stroke="Blue" StrokeThickness="0"/>
                <Button Canvas.Left="99" Canvas.Top="10" Height="22" Name="button1" Width="125" Click="button1_Click">Test create relation</Button>
                <Button Canvas.Left="233" Canvas.Top="10" Height="22" Name="button3" Width="124" Click="button3_Click">Test reroute relation</Button>
            </Canvas>
            
        </ScrollViewer>
        <Slider Canvas.Left="0" Canvas.Top="2" Height="500" Name="uiZoomSlider" Width="43" Value="1" Maximum="5" Minimum="0.1" Orientation="Vertical" ValueChanged="uiZoomSlider_ValueChanged" />
        <MyControl:UmlTree x:Name="UmlTreeView" Height="312.5" Canvas.Left="32" Canvas.Top="137.5" Width="162.5" />
        <TextBox Height="100" Name="debugConsole" AcceptsTab="True" AcceptsReturn="True" AutoWordSelection="True" TextWrapping="Wrap" Opacity="0.75" TextDecorations="None" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Visibility="Hidden" />
    </Canvas>
</MyControl:SelectableItemControl>
